package service

import (
	"context"                  // 导入上下文包，用于处理请求上下文
	"fmt"                      // 导入格式化包，用于错误信息格式化
	__ "order-srv/basic/proto" // 导入proto定义包
	"order-srv/handler/dao"
	"order-srv/untils"
	"strconv"
)

// 支付宝异步回调（仅回调）
func (s *Server) AlipayCallback(_ context.Context, _ *__.AlipayCallbackReq) (*__.AlipayCallbackResp, error) {
	return &__.AlipayCallbackResp{Message: "success"}, nil
}

// ListenOrder 监听接口
func (s *Server) ListenOrder(ctx context.Context, in *__.ListenOrderReq) (*__.ListenOrderResp, error) {
	// 记录监听订单请求日志
	fmt.Printf("开始监听订单，订单号: %s, 用户ID: %d\n", in.OrderNo, in.UserId)

	// TODO: 实现订单监听逻辑
	// 这里可以实现订单状态变化的实时通知功能

	// 返回监听成功信息
	return &__.ListenOrderResp{
		Id:      0, // 临时ID
		Message: "监听成功",
	}, nil
}

// 创建订单
func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {
	pay := untils.AliPay{}

	order, err := dao.CreateOrder(in)
	if err != nil {
		return nil, err
	}
	return &__.CreateOrderResp{
		Id:      order.Id,
		OrderNo: order.OrderNo,
		Pay:     pay.Pay(order.OrderNo, strconv.FormatInt(order.GoodsPrice, 10)),
	}, nil
}

// 订单详情
func (s *Server) DetailOrder(_ context.Context, in *__.DetailOrderReq) (*__.DetailOrderResp, error) {
	order, err := dao.DetailOrder(in)
	if err != nil {
		return nil, err
	}
	return &__.DetailOrderResp{
		Id:          order.Id,
		OrderNo:     order.OrderNo,
		AddressId:   order.AddressId,
		UserId:      order.UserId,
		GoodsId:     order.GoodsId,
		GoodsNum:    order.GoodsNum,
		GoodsName:   order.GoodsName,
		GoodsPrice:  order.GoodsPrice,
		OrderNum:    order.OrderNum,
		OrderPrice:  float32(order.OrderPrice),
		PayCType:    order.PayCType,
		PayStatus:   order.PayStatus,
		OrderStatus: order.OrderStatus,
	}, nil
}

// 订单列表
func (s *Server) ListOrder(_ context.Context, in *__.ListOrderReq) (*__.ListOrderResp, error) {
	err, list := dao.ListOrder(in)
	if err != nil {
		return nil, err
	}

	// 转换proto消息类型
	var orderItems []*__.ListGoods
	for _, item := range list {
		orderItems = append(orderItems, &__.ListGoods{
			Id:          item.Id,
			OrderNo:     item.OrderNo,
			AddressId:   item.AddressId,
			UserId:      item.UserId,
			GoodsId:     item.GoodsId,
			GoodsNum:    item.GoodsNum,
			GoodsName:   item.GoodsName,
			GoodsPrice:  item.GoodsPrice,
			OrderNum:    item.OrderNum,
			OrderPrice:  item.OrderPrice,
			PayCType:    item.PayCType,
			PayStatus:   item.PayStatus,
			OrderStatus: item.OrderStatus,
		})
	}

	return &__.ListOrderResp{
		List: orderItems,
	}, nil
}

// 删除订单
func (s *Server) DelOrder(_ context.Context, in *__.DelOrderReq) (*__.DelOrderResp, error) {
	err := dao.DelOrder(in)
	if err != nil {
		return nil, err
	}
	return &__.DelOrderResp{
		Message: "订单删除成功",
	}, nil
}

// 更新订单
func (s *Server) UpdateOrder(_ context.Context, in *__.UpdateOrderReq) (*__.UpdateOrderResp, error) {
	order, err := dao.UpdateOrder(in)
	if err != nil {
		return nil, err
	}
	return &__.UpdateOrderResp{
		Id:      order.Id,
		Message: "订单更新成功",
	}, nil
}

// 售后
func (s *Server) CreateSales(_ context.Context, in *__.CreateSalesReq) (*__.CreateSalesResp, error) {
	order, err := dao.AddSales(in)
	if err != nil {
		return nil, err
	}
	return &__.CreateSalesResp{
		SalesId: int64(order.Id),
	}, nil
}
